# -*- coding: utf-8 -*-

# tests/test_groff.py
# Part of ‘manpage’, a Python library for making Unix manual documents.
#
# Copyright © 2015–2016 Ben Finney <ben+python@benfinney.id.au>
#
# This is free software: see the grant of license at end of this file.

""" Unit tests for GNU troff markup behaviour. """

import sys
import os
import os.path
import collections
import unittest
import unittest.mock

__package__ = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
__import__(__package__)
sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import manpage.document
from .test_document import (
        NamedTuple_TestCaseMixIn,
        )


class TitleFields_TestCase(unittest.TestCase, NamedTuple_TestCaseMixIn):
    """ Test cases for class `TitleFields`. """

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()

        self.test_class = manpage.document.TitleFields
        self.test_args = collections.OrderedDict([
                ('title', "lorem"),
                ('section', "ipsum"),
                ('date', "dolor"),
                ('source', "sit"),
                ('manual', "amet"),
                ])


class GroffMarkup_escapetext_TestCase(unittest.TestCase):
    """ Test cases for method `GroffMarkup.escapetext`. """

    scenarios = [
            ('no-specials', {
                'test_args': {
                    'text': "Lorem ipsum, dolor sit amet."
                    },
                'expected_result': "Lorem ipsum, dolor sit amet.",
                }),
            ('backslash', {
                'test_args': {
                    'text': "Lorem \\ipsum",
                    },
                'expected_result': "Lorem \\[rs]ipsum",
                }),
            ('hyphen-default', {
                'test_args': {
                    'text': "Lorem-ipsum",
                    },
                'expected_result': "Lorem\\[hy]ipsum",
                }),
            ('hyphen-minus', {
                'test_args': {
                    'text': "Lorem-ipsum",
                    'hyphen': manpage.document.GroffMarkup.glyph.minus,
                    },
                'expected_result': "Lorem\\-ipsum",
                }),
            ('hyphen-specified', {
                'test_args': {
                    'text': "Lorem-ipsum",
                    'hyphen': "HYPHEN",
                    },
                'expected_result': "LoremHYPHENipsum",
                }),
            ]

    def test_returns_expected_text(self):
        """ Should return expected text for input. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.escapetext(**test_args)
                self.assertEqual(result, scenario['expected_result'])


class GroffMarkup_title_command_TestCase(unittest.TestCase):
    """ Test cases for method `GroffMarkup.title_command`. """

    def test_returns_expected_markup(self):
        """ Should return expected markup for input fields. """
        test_fields = manpage.document.TitleFields(
                title="lorem",
                section="ipsum",
                date="dolor",
                source="sit",
                manual="amet")
        result = manpage.document.GroffMarkup.title_command(test_fields)
        expected_result_fields_markup = " ".join([
                '"{0.title}"', '"{0.section}"',
                '"{0.date}"', '"{0.source}"', '"{0.manual}"']).format(
                    test_fields)
        expected_result = ".TH {fields}\n".format(
                fields=expected_result_fields_markup)
        self.assertEqual(result, expected_result)


class GroffMarkup_encoding_declaration_TestCase(unittest.TestCase):
    """ Test cases for method `GroffMarkup.encoding_declaration`. """

    scenarios = [
            ('encoding-UTF-8', {
                'test_args': {
                    'encoding': "utf-8",
                    },
                'expected_result': ".\\\" -*- coding: utf-8 -*-\n",
                }),
            ('encoding-unusual', {
                'test_args': {
                    'encoding': "LOREM",
                    },
                'expected_result': ".\\\" -*- coding: LOREM -*-\n",
                }),
            ]

    def test_returns_expected_text(self):
        """ Should return expected text for input. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.encoding_declaration(
                        **test_args)
                self.assertEqual(result, scenario['expected_result'])


class GroffMarkup_editor_hints_TestCase(unittest.TestCase):
    """ Test cases for method `GroffMarkup.editor_hints`. """

    scenarios = [
            ('encoding-UTF-8', {
                'test_args': {
                    'encoding': "utf-8",
                    },
                'expected_emacs_encoding': "coding: utf-8",
                'expected_vim_encoding': "fileencoding=utf-8",
                }),
            ('encoding-unusual', {
                'test_args': {
                    'encoding': "LOREM",
                    },
                'expected_emacs_encoding': "coding: LOREM",
                'expected_vim_encoding': "fileencoding=LOREM",
                }),
            ]

    def test_lines_start_with_comment_leader(self):
        """ All lines of text should start with comment leader. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.editor_hints(
                        **test_args)
                for (lineindex, line) in enumerate(result.splitlines()):
                    with self.subTest(line=lineindex):
                        self.assertTrue(line.startswith(".\\\""))

    def test_returns_emacs_encoding(self):
        """ Should return Emacs hint for text encoding. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.editor_hints(
                        **test_args)
                self.assertIn(
                        scenario['expected_emacs_encoding'], result)

    def test_returns_emacs_major_mode(self):
        """ Should return Emacs hint for major mode. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.editor_hints(
                        **test_args)
                expected_hint = "mode: nroff"
                self.assertIn(expected_hint, result)

    def test_returns_vim_encoding(self):
        """ Should return Vim hint for text encoding. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.editor_hints(
                        **test_args)
                self.assertIn(
                        scenario['expected_vim_encoding'], result)

    def test_returns_vim_filetype(self):
        """ Should return Vim hint for file type. """
        for (scenario_name, scenario) in self.scenarios:
            with self.subTest(scenario=scenario_name):
                test_args = scenario['test_args']
                result = manpage.document.GroffMarkup.editor_hints(
                        **test_args)
                expected_hint = "filetype=nroff"
                self.assertIn(expected_hint, result)


# This is free software: you may copy, modify, and/or distribute this work
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of that license or any later version.
#
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details,
# or view it online at <URL:https://www.gnu.org/licenses/gpl-3.0.html>.


# Local variables:
# coding: utf-8
# mode: python
# End:
# vim: fileencoding=utf-8 filetype=python :
